package br.com.fatecjp.pooai.gerenciador.taxi.ui;

import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.WindowConstants;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class ConfiguradorAmbiente extends JFrame {

	private static final long serialVersionUID = 1074583224757153298L;

	private static final Dimension PREFERRED_SIZE = new Dimension(300, 100);

	private int layoutRow;
	private JSpinner spinnerContadorTaxis;
	private SpinnerNumberModel modeloSpinnerContadorTaxis;
	private JButton botaoOk;

	public ConfiguradorAmbiente() {
		super();
		this.inicializar();
	}

	private void inicializar() {
		this.inicializarLayout();
		this.inicializarComponentes();
	}

	private void inicializarLayout() {

		this.setLayout(new FormLayout(
				"5px, right:80px, 5px, fill:100px:grow, 5px",
				"5px, 23px, 5px, 23px, 5px"));
		this.layoutRow = 2;

	}

	private void inicializarComponentes() {

		this.setTitle("Configurações do Ambiente");
		this.setPreferredSize(PREFERRED_SIZE);
		this.setSize(PREFERRED_SIZE);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setResizable(false);

		CellConstraints cc = new CellConstraints();

		this.add(new JLabel("Qtde Táxis"), cc.xy(2, this.layoutRow));

		this.modeloSpinnerContadorTaxis = new SpinnerNumberModel(5, 1, 10, 1);
		this.spinnerContadorTaxis = new JSpinner(this.modeloSpinnerContadorTaxis);
		this.add(this.spinnerContadorTaxis, cc.xy(4, this.layoutRow));
		this.layoutRow += 2;

		this.botaoOk = new JButton("Iniciar");
		this.add(this.botaoOk, cc.xy(4, this.layoutRow,
				CellConstraints.RIGHT, CellConstraints.CENTER));

	}

	public void addSubmitButtonActionListener(ActionListener listener) {
		this.botaoOk.addActionListener(listener);
	}

	public Integer getQuantidadeTaxis() {
		Number value = (Number) this.modeloSpinnerContadorTaxis.getValue();
		return value.intValue();
	}

	@Override
	public void setVisible(boolean b) {
		super.setVisible(b);
		this.setLocationRelativeTo(this.getParent());
	}

}
